var privilegeList; // 权限列表
var accType;
function pridIsExist(prid) {
	if ($.inArray(prid, privilegeList) == -1){
		return false;
	}else{
		return true;
	}
}
function getLocationUrl() {
	var protocol = window.location.protocol;// 获取当前访问协议
	var host = window.location.host; // 获取IP和端口
	return protocol + '//' + host+"/svr/";
}
/** 系统公共变量* */
var ajaxurl = "emservlet";// 设置call的访问地址
var userId;// 登录的用户id
var language;// 当前选择的语言版本
var mainSrc;// mainView加载的src
var sessionManagerId; // 管理员ID
var sessionManagerName;// 管理员名称
//function callSvr(service, parameter, success, openProgerssLoad) {
//	var sync=true;
//	if(typeof(openProgerssLoad)=="undefined"){
//		openProgerssLoad=true;
//		sync=false;
//	}else{
//		sync=!openProgerssLoad
//	}
//	$.ajax({
//		type : 'POST',
//		url : getLocationUrl() + service,
//		data : parameter,
//		dataType : 'json',
//		async : sync,
//		beforeSend : function(xhr) {
//			if (openProgerssLoad) {
//				$("#mask").show();
//			}
//		},
//		error : function(XMLHttpRequest, textStatus, errorThrown) {
//			console.log("请求失败:"+JSON.stringify(XMLHttpRequest)+","
//				+JSON.stringify(textStatus)+","+JSON.stringify(errorThrown));
//			alert("服务器忙,请稍后重试");
//		},
//		success : function(data) {
//			if (data.err == -9||data.err==10) {
//				window.location.href = "/login.html";
//			}else{
//				success(data);
//			}
//		},
//		complete:function(){
//			if (openProgerssLoad) {
//				$("#mask").hide();
//			}
//		}
//	});
//}
// 获取层或表单组件的值并转换成json
$.fn.formToJson = function() {
	var values = {};
	this.find("*[name]").each(function(index, element) {
		/*var elementName = element.name;
		 $(this).find("[getData]").each(function(index, element) {
		 if (elementName != undefined && elementName != "") {
		 var result = element.getData.toString();
		 values[elementName] == callFunc(result);
		 }
		 });*/
		if (element.name != undefined && element.name != "") {
			var elementName = element.name;
			var type = element.type;
			if (element.type == "radio" || type == "checkbox") {
				if (element.checked) {
					if (values[elementName] == undefined || values[elementName] == '') {
						values[elementName] = element.value.trim();
					} else {
						values[elementName] = values[elementName]+ "," + element.value.trim();
					}
				} else {
					if (values[elementName] == undefined|| values[elementName] == '') {
						values[elementName] = '';
					}
				}
			} else {
				if (values[elementName] == undefined) {
					/*if (typeof ($(this).attr("isDate")) != "undefined" ) {
					 values[elementName] = element.value.trim();
					 } else if (typeof ($(this).attr("isNum")) != "undefined") {
					 values[elementName] = element.value.trim();
					 } else {
					 values[elementName] = element.value.trim();
					 }*/
					if(element.value !="undefined" && element.value !="null"){
						values[elementName] = element.value.trim();
					}else{
						values[elementName] = "";
					}
				} else {
					values[elementName] = values[elementName]+ "," + element.value.trim();
				}
			}
		}
	});
	return values;
};
// 获取表单对应的属性
$.fn.getJSONForm = function() {
	var val = {};
	$(this).find(":visible [name]").each(function(index, element) {
		mes = $(this).parent().prev().text();
		if (mes.indexOf("*") >= 0) {
			mes = mes.substr(1, mes.length - 2).trim();
		} else {
			mes = mes.substr(0, mes.length - 1).trim();
		}
		if (element.name != undefined && element.name != "") {
			var elementName = element.name;
			var type = element.type;
			if (element.type == "radio" || type == "checkbox") {
				if (element.checked) {
					if (elementName == undefined || elementName == '') {
						val[mes] = elementName;
					} else {
						val[mes] = val[mes] + "," + elementName;
					}
				} else {
					if (elementName == undefined || elementName == '') {
						val[mes] = elementName;
					}
				}
			} else {
				if (elementName == undefined) {
					val[mes] = elementName;
				} else {
					val[mes] = elementName;
				}
			}
		}
	});
	return valuse;
};

// 获取整个html设置了name属性的组件的值并转换成json
function bodyToJson() {
	var valuse = {};
	$("*[name]").each(
		function(index, element) { // 所有包含name的节点
			if (element.name != undefined && element.name != "") {
				var elementName = element.name;
				var type = element.type;
				if (element.type == "radio" || type == "checkbox") {
					if (element.checked) { // 如果选择
						if (valuse[elementName] == undefined
							|| valuse[elementName] == '') {
							valuse[elementName] = element.value.trim();
						} else {
							valuse[elementName] = valuse[elementName] + ","
								+ element.value.trim();
						}
					} else {// 没有被选择
						if (valuse[elementName] == undefined
							|| valuse[elementName] == '') {
							valuse[elementName] = '';
						}
					}
				} else {
					if (valuse[elementName] == undefined) {
						valuse[elementName] = element.value.trim();
					} else {
						valuse[elementName] = valuse[elementName] + ","
							+ element.value.trim();
					}
				}
			}
		});
	return valuse;
}

// jsons为对象里面的所以控件赋值
$.fn.jsonToform = function(json) {
	this.find("*[name]").each(
		function(index, element) {
			for ( var key in json) {
				if (element.name != undefined
					&& element.name.toLowerCase() == key
						.toLowerCase()) {
					if (element.type == "checkbox") {
						if (json[key] != '') {
							var checkValue = (json[key]).split(",");
							for ( var str in checkValue) {
								if (element.value == checkValue[str]) {
									element.checked = true;
								}
							}
						}
					} else if (element.type == "radio") {
						if (element.value == json[key]) {
							element.checked = true;
						}
					} else if (element.type == "select") {
						if (element.value == json[key]) {
							element.checked = true;
						}
					} else {
						if(json[key]!=null && json[key] !=""){
							element.value = json[key];
						}
						/*if (typeof ($(element).attr("isNum")) == "undefined") {
						 element.value = json[key];
						 } else {
						 if (json[key] != "0") {
						 element.value = json[key];
						 }
						 }*/

					}
					break;
				}
			}
		});
};
// 设置表单只读
$.fn.elementToUnable = function() {
	$(this).find('[name]').each(function(index, element) {
		if (element.name != undefined && element.name != "") {
			if (element.type == "radio") {
				$(this).attr("disabled", true);
			} else if ((element.type).indexOf("select") >= 0) {
				$(this).attr("disabled", true);
			} else {
				$(this).attr("readonly", "readonly");
			}
		}
	});
};

// 为整个html页面里面的控件赋值
function jsonToBody(json) {
	$("*[name]").each(function(index, element) {
		for ( var key in json) {
			if (element.name == key) {
				if (element.type == "checkbox") {
					if (json[key] != '') {
						var checkValue = (json[key]).split(",");
						for ( var str in checkValue) {
							if (element.value == checkValue[str]) {
								element.checked = true;
							}
						}
					}
				} else if (element.type == "radio") {
					if (element.value == json[key]) {
						element.checked = true;
					}
				} else {
					element.value = json[key];
				}
				break;
			}
		}
	});
}

// 获取访问的项目路径

// 去除字符串前后空格
String.prototype.trim = function() {
	return this.replace(/[(^\s+)(\s+$)<>{}]/g, '');
};
/*
 * function loadDiv() { if ($('div[src]').size() == 0) { // 调用页面初始化函数
 * $("[init]").each(function() { eval($(this).attr("init"));
 * $(this).removeAttr("init"); }); } $('div[src]').each(function() {
 * $(this).load($(this).attr('src'), '', function() { $(this).removeAttr("src");
 * loadDiv(); }); }); }
 */
/*
 * function lang(key) { if (key == null || key == "") { return ""; } var fun =
 * "jQuery.i18n.prop("; for (var i = 0; i < arguments.length; i++) { if (typeof
 * (arguments[i]) == "string") { fun += "'" + arguments[i] + "',"; } } fun =
 * fun.substring(0, fun.lastIndexOf(",")); fun += ")"; return eval(fun); }
 */
/** Cookie 相关* */
function addCookie(name, value, expiresDays,path) {
	var cookieString = name + "=" + escape(value);
	// 判断是否设置过期时间
	if (expiresDays > 0) {
		var date = new Date();
		date.setTime(date.getTime() + (expiresDays * 3600 * 1000 * 24));
		cookieString = cookieString + "; expires=" + date.toGMTString();
	}
	if(path){
		cookieString=cookieString+";path="+path;
	}
	document.cookie = cookieString;
}
function getCookie(name) {
	var strCookie = document.cookie;
	var arrCookie = strCookie.split("; ");
	for (var i = 0; i < arrCookie.length; i++) {
		var arr = arrCookie[i].split("=");
		if (arr[0] == name)
			return arr[1];
	}
	return "";
}
function deleteCookie(name) {
	var date = new Date();
	date.setTime(date.getTime() - 10000);
	document.cookie = name + "=v; expires=" + date.toGMTString();
}
function clearCookie() {
	var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
	if (keys) {
		for (var i = keys.length; i--;)
			document.cookie = keys[i] + '=0;expires='
				+ new Date(0).toUTCString();
	}
}

/*******************************************************************************
 * 调用函数，如果是变量需要用{}将其包裹，包含了变量参数时需要在callFunc外包裹eval(callFunc(...)) 如果参数都是String
 * 没有包含变量参数则会自动调用。
 ******************************************************************************/
function callFunc(fun) {
	if (arguments.length > 1) {
		var call = true;
		var fun = arguments[0].substring(0, arguments[0].lastIndexOf("(") + 1);
		for (var i = 1; i < arguments.length; i++) {
			if (typeof (arguments[i]) == "string") {
				if (arguments[i].indexOf("}") > -1) {
					call = false;
					fun += arguments[i].substring(
							arguments[i].indexOf("{") + 1, arguments[i]
								.lastIndexOf("}"))
						+ ",";
				} else {
					fun += "'" + arguments[i] + "',";
				}
			}
		}
		fun = fun.substring(0, fun.lastIndexOf(","));
		fun += ")";
	}
	if (call) {
		eval(fun);
	}
	return fun;
}
function popWindowShow(options) {
	var dialogGroup = $("<div class='dialogGroup'></div>");
	var shadeDiv = $("<div class='dialogShade'></div>").appendTo(dialogGroup);
	var dialogMid = $("<div class='dialogMid'></div>").appendTo(dialogGroup);
	var dialogMain = $("<div class='dialogMain'></div>").appendTo(dialogMid);
	var dialogTitle = $("<div class='portlet-title' style='text-align:left'><span class='glyphicon glyphicon-edit' aria-hidden='true' style='margin-right:5px'></span></div>")
		.appendTo(dialogMain);
	var titleContext = $("<span></span>").appendTo(dialogTitle);
	var exitBtn = $(
		"<div class='pull-right' style='margin-right:10px;cursor:pointer;'><span class='glyphicon glyphicon-remove'></span><div>").appendTo(dialogTitle);
	var dialogContent = $("<div style='padding:5px 10px'></div>").appendTo(dialogMain);
	var dialogBtnDiv = $("<div class='col-xs-12 text-center' style='margin-bottom:10px'></div>")
	var saveBtn = $("<button class='btn btn-info btn-sm' style='margin-right:5px'>确定</button>").appendTo(dialogBtnDiv);
	var callbackBtn = $("<button class='btn btn-default btn-sm'>取消</button>")
		.appendTo(dialogBtnDiv);
	var pop = {
		opts : {
			popWidth : "",// 窗口宽度
			popHeight : "",
			position : "center",// 窗口位置(默认居中)
			title : "新建窗口",// 窗口title
			content : "",// 弹窗内容
			src : "",// 弹窗引用页面
			zindex : 500,// 弹窗显示层级
			btnShow : true,// 是否显示"确定/取消"按钮
			id : ""
		},
		exitBtn : exitBtn,
		saveBtn : saveBtn,
		callbackBtn : callbackBtn,
		dialogGroup : dialogGroup,
		dialogMid : dialogMid,
		dialogBtnDiv : dialogBtnDiv,
		dialogMain : dialogMain,
		dialogContent : dialogContent,
		shadeDiv:shadeDiv,
		titleContext:titleContext,
		init : function(options) {
			var opts = $.extend({},this.opts,options);
			var nowId = Math.floor(Math.random() * 999 + 1);
			if (nowId < 10) {
				opts.id = "Dialog00" + nowId;
			} else if (nowId < 100) {
				opts.id = "Dialog0" + nowId;
			} else {
				opts.id = "Dialog" + nowId;
			}
			this.dialogGroup.attr("id", opts.id);
			this.callbackBtn.click(function() {
				$("#" + opts.id).remove();
			});
			this.exitBtn.click(function() {
				$("#" + opts.id).remove();
			});
			this.shadeDiv.css("z-index",opts.zindex-1);
			this.dialogMain.css({"z-index":opts.zindex,"width":opts.popWidth});
			this.titleContext.html(opts.title);
			if (opts.position == "center") {
				this.dialogMid.css({
					"vertical-align" : "middle",
					"text-align" : "center"
				});
			} else if (opts.position == "left_down") {
				this.dialogMid.css({
					"vertical-align" : "bottom",
					"text-align" : "left"
				});
				this.dialogMain.css({
					"right" : "50px",
					"bottom" : "50px"
				});
			} else if (opts.position == "right_down") {
				this.dialogMid.css({
					"vertical-align" : "bottom",
					"text-align" : "right"
				});
				this.dialogMain.css({
					"right" : "50px",
					"bottom" : "50px"
				});
			}
			if (opts.btnShow) {
				this.dialogBtnDiv.appendTo(this.dialogMain);
			}
			this.dialogContent.load(opts.src);
			this.opts=opts;
			$("body").append(this.dialogGroup);
		},
		exit:function(){
			$("#"+this.opts.id).remove();
		}
	}
	pop.init(options);
	return pop;
}
// 关闭窗口的方法(方便动态调用)
function initOrg() {
	call("orgImpl.getOrgListByPid", "", function(result) {
		for (var i = 0; i < result.length; i++) {
			$("[name='orgId']").append(
				"<option value='" + result[i].orgId + "'>" + result[i].name
				+ "</option>");
		}
	});
}
$.fn.jsonTotable = function(json) {
	this.find("*[name]").each(function() {
		for ( var key in json) {
			if ($(this).attr("name") == key) {
				$(this).html(json[key]);
				break;
			}
		}
	});
};
function getParameter(name) {
	var paramStr = location.href;
	if (paramStr.length == 0)
		return null;
	if (paramStr.charAt(0) != '?')
		return null;
	paramStr = unescape(paramStr);
	paramStr = paramStr.substring(1);
	if (paramStr.length == 0)
		return null;
	var params = paramStr.split('&');
	for (var i = 0; i < params.length; i++) {
		var parts = params[i].split('=', 2);
		if (parts[0] == name) {
			if (parts.length < 2 || typeof (parts[1]) == "undefined"
				|| parts[1] == "undefined" || parts[1] == "null")
				return "";
			return parts[1];
		}
	}
	return null;
}
$.fn.poptip = function(opts) {
	var xy = $(this).offset();
	var x = Math.abs(($(this).outerWidth() - 127) / 2);
	var offsetx = xy.left - x;
	var offsety = xy.top + $(this).height() + 3;
	var popDiv, nowId;
	if (typeof ($(this).attr("data-popid")) == "undefined") {
		nowId = "poptip" + Math.floor(Math.random() * 999 + 1);
		$(this).attr("data-popid", nowId);
		popDiv = $("<div>", {
			"class" : "poptip",
			"style" : "z-index:9999;position:fixed;display:none",
			"id" : nowId
		});
		popDiv
			.html("<div class='SD-tipbox' style='width:110px'><div class='cntBox'><button id='carg0'class='btn btn-xs btn-info' style='margin-right:5px'>左笼</button><button id='carg1'class='btn btn-xs btn-info'>右笼</button></div><div class='SD-tipbox-direction SD-tipbox-up'><em>&#9670;</em><span>&#9670;</span></div></div></div></div>");
		$("body").append(popDiv);
		$(popDiv).offset({
			top : offsety,
			left : offsetx
		});
		if (opts.carg0.term == "(左)-")
			popDiv.find("#carg0").prop("disabled", true);
		if (opts.carg1.term == "(右)-")
			popDiv.find("#carg1").prop("disabled", true);
		popDiv.find("#carg0").data("term", opts.carg0.term).on("click",
			function() {
				opts.carg0.ck();
				popDiv.hide();
			});
		popDiv.find("#carg1").data("term", opts.carg0.term).on("click",
			function() {
				opts.carg1.ck();
				popDiv.hide();
			});
	} else {
		nowId = $(this).attr("data-popid");
		popDiv = $("#" + nowId);
	}

	$(this).click(function(event) {
		$(".poptip").hide();
		popDiv.show();
		event.stopPropagation();
	});
	$("body").click(function(event) {
		popDiv.hide();
	});
};
// 站内信
function GetMessage() {
	var IntervalMessage = function() {
		var obj = new Object();
		obj.start = function() {
			obj.Interval = setInterval(function() {
				if (em.myId != null && typeof (em.myId) != "undefined") {
					callSvr("message/getNoSendMessage", {
						"accid" : em.myId
					}, function(data) {
						$("#mesSpan").html("");

						if (data != null && typeof (data) != "undefined") {
							if (data.err == 9) {
								window.clearInterval(obj.Interval);
								return;
							}
							if (data.length > 0)
								$("#mesSpan").html(data.length);
						} else {
							window.clearInterval(obj.Interval);
						}
					}, false);
				}
			}, 1860000);
		};
		obj.stop = function() {
			if (typeof (obj.Interval) != "undefined")
				window.clearInterval(obj.Interval);
		};
		return obj;
	};
	(new IntervalMessage()).start();
}
;
(function($, window, document, undefined) {
	var TreeViewFun = function(ele, jsondata, fun) {
		this.$elements = ele;
		this.jsondata = jsondata;
		this.fun = fun;
	};
	TreeViewFun.prototype.initdata = function() {
		var self = this;
		for (var i = 0; i < self.jsondata.length; i++) {
			var json = self.jsondata[i];
			var parentele = self.$elements.find($('[data-value=' + json.pid
				+ ']'));
			var li = $("<li data-value=" + json.OrgId + "><a>" + json.Name
				+ "</a></li>");
			if (i != 0) {
				if (parentele.children('ul').length > 0) {
					parentele.children('ul').append(li);
				} else {
					var childrenUl = $("<ul></ul>");
					childrenUl.append(li);
					parentele.append(childrenUl);
				}
			} else {
				self.$elements.append(li);
			}
		}
		self.$elements.attr('data-activeorgid', self.jsondata[0].orgid);
	};
	TreeViewFun.prototype.init = function() {
		var self = this;
		self.$topLevelDom = self.$elements.children('li');
		self.$allLevelDom = self.$elements.find('li');
		self.$elements.addClass('tree-list');
		self.$allLevelDom.not(self.$topLevelDom).hide();
		self.$allLevelDom.each(function() {
			var index = $(this).index();
			$(this).prepend('<span class="tree-close"></span>');
			var swith = $('<span class="swith collapsed"></span>').prependTo(
				$(this));
			if (index == 0 && $(this).siblings('li').length == 0) {
				swith.addClass('noline-close');
			} else if (index == 0 && $(this).siblings('li').length != 0) {
				swith.addClass('root-close');
				$(this).children('ul').addClass('line');
			} else if (index > 0 && index != $(this).siblings('li').length) {
				swith.addClass('center-close');
				$(this).children('ul').addClass('line');
			} else {
				swith.addClass('bottom-close');
			}

			$(this).children('a').on('click', function(e) {
				self.$allLevelDom.find('a').removeClass('active-li');
				$(this).addClass('active-li');
				self.fun($(this).parent('li'));
				e.stopPropagation();
			});
		});
		self.$allLevelDom.children('span.swith').on('click', function(e) {
			if ($(this).hasClass('collapsed')) {
				self.expand($(this).parent());
				e.stopPropagation();
			} else {
				self.collapse($(this).parent());
				e.stopPropagation();
			}
		});
	};
	TreeViewFun.prototype.expand = function(expandLi) {
		var root_icon = expandLi.children('span.swith');
		root_icon.removeClass('collapsed').addClass('expanded');
		root_icon.next().removeClass().addClass('tree-open');
		if (root_icon.hasClass('noline-close'))
			root_icon.removeClass('noline-close').addClass('noline-open');
		if (root_icon.hasClass('root-close'))
			root_icon.removeClass('root-close').addClass('root-open');
		if (root_icon.hasClass('center-close'))
			root_icon.removeClass('center-close').addClass('center-open');
		if (root_icon.hasClass('bottom-close'))
			root_icon.removeClass('bottom-close').addClass('bottom-open');
		expandLi.children('ul').children('li').show();
	};
	TreeViewFun.prototype.collapse = function(collapseLi) {
		var root_icon = collapseLi.children('span.swith');
		root_icon.removeClass('expanded').addClass('collapsed');
		root_icon.next().removeClass().addClass('tree-close');
		if (root_icon.hasClass('noline-open'))
			root_icon.removeClass('noline-open').addClass('noline-close');
		if (root_icon.hasClass('root-open'))
			root_icon.removeClass('root-open').addClass('root-close');
		if (root_icon.hasClass('center-open'))
			root_icon.removeClass('center-open').addClass('center-close');
		if (root_icon.hasClass('bottom-open'))
			root_icon.removeClass('bottom-open').addClass('bottom-close');
		collapseLi.children('ul').children('li').hide();
	};
	$.fn.TreeView = function(jsondata, fun) {
		var treeview = new TreeViewFun(this, jsondata, fun);
		treeview.initdata();
		treeview.init();
		return treeview;
	};
})(jQuery);

